//
//  HYFHomeController.swift
//  MyWeibo
//
//  Created by 黄勇锋 on 16/7/19.
//  Copyright © 2016年 黄勇锋. All rights reserved.
//

import UIKit

private let cellID = "homeCell"

class HYFHomeController: HYFVisitController {
    
    //声明属性
    var homeView:HYFVisitorView?
    
    //懒加载数组
    lazy var statusesListViewModel:HYFStatusListViewModel = HYFStatusListViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //如果用户没有登陆,那么返回一个 游客界面.
        if !isLogin {
            
            visitorView?.setupVisitorViewInfo(nil, title: nil)
            
            return
        }
        
        setupTabelViewInfo()
        getHomeData()
    }
    
    func setupTabelViewInfo(){
        
        //设置数据源代理
        tableView.dataSource = self
        tableView.delegate = self
        
         //MARK: 添加下拉刷新提示label ---> 插入到导航栏下方
        navigationController!.view.insertSubview(pullDownTipLabel, belowSubview: navigationController!.navigationBar)
        
        
        //注册 cell
        tableView.registerClass(HYFStatusCell.self, forCellReuseIdentifier: cellID)
        //设置tableViewCell 的高度
        //tableView.rowHeight = 200
        
        //设置tableView 的预估行高,越接近真实高度越好
        tableView.estimatedRowHeight = 200
        //设置footView
        tableView.tableFooterView = indicatorView
        
        //添加刷新控件
        tableView.addSubview(refreshControl)
        
        //去掉 tabelView 的分割线
        tableView.separatorStyle = .None
        
        //添加事件
        refreshControl.addTarget(self, action: #selector(HYFHomeController.refreshAction), forControlEvents: UIControlEvents.ValueChanged)
    }
    
    
    func refreshAction(){
        getHomeData()
    }
    
    //懒加载菊花
    private lazy var indicatorView:UIActivityIndicatorView = {
        
       let view = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
        
        view.color = ThemeColor
        
        return view
    }()
    
    //系统下拉刷新
    private lazy var refreshControl:SHRefreshControl = SHRefreshControl()
    
    //下拉刷新提示按钮
    private lazy var pullDownTipLabel:UILabel = {
        
     let lab = UILabel(textColor: UIColor.whiteColor(), textFont: 12)
     lab.frame = CGRect(x: 0, y: 64 - 30, width: SCREENW, height: 30)
     lab.textAlignment = .Center
     lab.backgroundColor = ThemeColor
     lab.hidden = true
        
      return lab
    }()
}

extension HYFHomeController:UITableViewDataSource,UITableViewDelegate {
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return statusesListViewModel.statusList.count
        
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(cellID, forIndexPath: indexPath) as! HYFStatusCell
        
        cell.statusViewModel = statusesListViewModel.statusList[indexPath.row]
        
        return cell
    }
    
    //cell 将要出现的时候
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        
        //判断有没有到最后一个
        if indexPath.row == statusesListViewModel.statusList.count - 1 && !indicatorView.isAnimating(){
            
            //开始动画
            indicatorView.startAnimating()
            
            getHomeData()
        }
    }
}

//请求微博数据
extension HYFHomeController{
    
    func getHomeData(){
        
        statusesListViewModel.loadHomeData(indicatorView.isAnimating()) { (isSuc,count) in
            
            //停止动画
            self.endRefreshing()
            
            
            if !isSuc {
                
                print("请求数据失败")
                
                return
            }
            //刷新UI
            self.tableView.reloadData()
            
            //下拉刷新
            if count >= 0{
                
               self.setPullDownTipLabelAnim(count)
                
            }
            
        }
    }
    
    func endRefreshing(){
        
        self.indicatorView.stopAnimating()
        self.refreshControl.endRefreshing()
        
    }
    
    //设置微博提示动画
    func setPullDownTipLabelAnim(count:Int){
        
        //如果当前的动画也就是显示,就不在进行动画
        if pullDownTipLabel.hidden == false {
            
            return
        }
        
        //显示控件
        pullDownTipLabel.hidden = false
        var name = ""
        
        if count == 0 {
            
            name = "已经是最新的微博数据啦"
        }else{
            
            name = "更新了\(count)条微博"
        }
        
        //设置text
        pullDownTipLabel.text = name
        
        //设置动画
        UIView.animateWithDuration(0.5, animations: { 
            
            self.pullDownTipLabel.transform = CGAffineTransformMakeTranslation(0, 30)
            
            }) { (_) in
                
              UIView.animateWithDuration(1, animations: {
                
                self.pullDownTipLabel.transform = CGAffineTransformIdentity
                
                }, completion: { (_) in
                    
                    //完成后隐藏
                    self.pullDownTipLabel.hidden = true
              })
        }
    }
}





